/**

common test class to use for testing the nonWebSQL drivers if they are able to correctly implement the
evaluation relational expressions (i.e., SELECT commands) and statements (INSERT, UPDATE, DELETE commands).

	crcDB - global variable defined in app/SQL.js
	initDB - function defined in app/SQL.js; simulated in the test case below

*/


RelationalOperationsCommonTest = Class.extend(function(){
	

	var tests = {} //a json object consisting of test cases (i.e., js functions)
	    ,data = CommonTestData.data();
	
	
	HPI.apply(tests, {
	
		localStorageDriver : "WebSQL"
	   
	    ,initDB : function(){
		
			
			var shortName = 'crcInv1';
			var version = '1.0';
			var displayName = 'CRC Database';
			var maxSize = 1024 * 1024 * 5; // in bytes		
			
			if( crcDB !== null ) return;
			
			if( this.localStorageDriver != WebSQLAPICommonTest.WEB_SQL ){
			
				crcDB = HPI.SQLDatabase.open({
					shortName : shortName
				   ,dbName : shortName
				   ,version : version
				   ,displayName : displayName
				   ,maxSize  : maxSize
				   ,storageDriver : this.localStorageDriver
				   ,mode : 'sync' //set in synchronous mode for now since test classes is assuming sync. mode. but only localStorage driver will recognise this.
				});
				
				return;
			}
			
			try {

				if (!window.openDatabase) {
					alert('not supported');
				} else {
					var myDB = openDatabase(shortName, version, displayName, maxSize);
					//alert("database created");
				}
			} catch (e) {
				// Error handling code goes here.
				if (e == INVALID_STATE_ERR) {
					// Version number mismatch.
					alert("Invalid database version.");
				} else {
					alert("Unknown error " + e + ".");
				}
				return;
			}
			
			crcDB = myDB;
		
		}
	
		//this function is similar to the ExecuteReader* functions in the app.
		,executeReader : function(query, db, callback, _args){
				
			_args = _args || [];
			db.transaction(
				function (transaction) {
					transaction.executeSql(query, _args, callback, function (transaction, error) {
						callback(transaction, error);
					});
				}
			);
			
		}
		
		//gets the data of the SQLResult object
		,srData : function(sqlResult){
			
			var data = [];
			
			for(var i = 0, len = sqlResult.rows.length; i < len; i++){
				data = data.concat([sqlResult.rows.item(i)]);
			}
			return data;
			
		}
	
	});
	
	if( data["testOnly"] != undefined  ){
		
		var tstOnly = HPI.isArray(data["testOnly"]) ? data["testOnly"] : [data["testOnly"]];
		var _data = {};
		for(var i = 0, len = tstOnly.length; i < len; i++){
			
			if( data[tstOnly[i]] != undefined ){				
				_data[tstOnly[i]] = data[tstOnly[i]];
			}
			
		}
		
		data   = _data;
		
	}
	
	for(var index in data){	
	
		var _tstParams = data[index];
		
		//if( undefined == _tstParams["query"] && undefined == _tstParams.testFn ) continue;
	
		if( undefined != _tstParams.testFn ) {
		
			tests["test" + index.toString()] = function(testParams){
		
				return testParams.testFn;
				
			}.apply(this, [_tstParams]);
			
			continue;
		
		}
		
		if( undefined == _tstParams["query"] ) continue;
		
		tests["test" + index.toString()] = function(testParams){
			
			
			return function(){
				
				if( typeof testParams.preTest == 'function' ){
					testParams.preTest.apply(this, []);
				}
				
				if( typeof testParams.evaluateResult == 'function' ){
					var testIns = this;
					this.executeReader(testParams.query, crcDB, function(t, r_or_e){
						assertTrue(testParams.evaluateResult.apply(testIns, [t, r_or_e, testParams]));
					});
				}
				
				if( typeof testParams.postTest == 'function' ){
					testParams.postTest.apply(this, []);
				}
			}
			
		}.apply(this, [_tstParams]);

	}

	return tests;

}());


/*
RelationalOperationsCommonTest = Class.extend({

	localStorageDriver : "WebSQL"

	//simulation of initDB found in app/SQL.js
	//necessary to do this since we will be adding the logic here as to which local storage "driver" to use
    ,initDB : function(){
		
		
		var shortName = 'crcInv1';
		var version = '1.0';
		var displayName = 'CRC Database';
		var maxSize = 1024 * 1024 * 5; // in bytes		
		
		if( crcDB !== null ) return;
		
		if( this.localStorageDriver != WebSQLAPICommonTest.WEB_SQL ){
		
			crcDB = HPI.SQLDatabase.open({
				shortName : shortName
			   ,dbName : shortName
			   ,version : version
			   ,displayName : displayName
			   ,maxSize  : maxSize
			   ,storageDriver : this.localStorageDriver
			});
			
			return;
		}
		
		try {

			if (!window.openDatabase) {
				alert('not supported');
			} else {
				var myDB = openDatabase(shortName, version, displayName, maxSize);
				//alert("database created");
			}
		} catch (e) {
			// Error handling code goes here.
			if (e == INVALID_STATE_ERR) {
				// Version number mismatch.
				alert("Invalid database version.");
			} else {
				alert("Unknown error " + e + ".");
			}
			return;
		}
		
		crcDB = myDB;
	
	}
	
	//this function is similar to the ExecuteReader* functions in the app.
	,executeReader : function(query, db, callback, _args){
			
		_args = _args || [];
		db.transaction(
			function (transaction) {
				transaction.executeSql(query, _args, callback, function (transaction, error) {
					callback(transaction, error);
				});
			}
		);
		
	}
	

	,test1 : function(){
	
		this.initDB();
		var query = "SELECT a, b FROM tbl1;"
		this.executeReader(query, crcDB, function(t, r_or_e){
			console.log(r_or_e);
		});
	}
});
*/